package com.wcs.exam.common.server.upload;

import com.wcs.exam.common.base.BaseBiz;
import com.wcs.exam.common.base.exception.BaseException;
import com.wcs.exam.common.enums.StoragePlatformEnum;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

@RequiredArgsConstructor
@Component
public class UploadService extends BaseBiz {

    @NotNull
    private final Map<String, UploadFace> uploadFaceMap;

    /**
     * 图片上传
     *
     * @return 图片url
     */
    public String uploadPic(MultipartFile file, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(upload.getStoragePlatform());
        return uploadFace.uploadPic(file, upload);
    }

    /**
     * 图片Base64字符串上传
     *
     * @param imgBase64 图片Base64字符串
     * @param upload    上传配置
     * @return 图片地址
     */
    public String uploadPicBase64(String imgBase64, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(upload.getStoragePlatform());
        return uploadFace.uploadPicBase64(imgBase64, upload);
    }

    /**
     * 文档上传
     *
     * @return 文档url
     */
    public String uploadDoc(MultipartFile file, Upload upload, Boolean isPublicRead) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(upload.getStoragePlatform());
        return uploadFace.uploadDoc(file, upload, isPublicRead);
    }

    /**
     * 文档上传
     *
     * @return 文档url
     */
    public String uploadDoc(File file, String fileName, Upload upload, Boolean isPublicRead) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(upload.getStoragePlatform());
        return uploadFace.uploadDoc(file, fileName, upload, isPublicRead);
    }


    /**
     * 删除文档
     *
     * @param fileUrl 删除文件地址
     * @param upload  上传配置
     */
    public void delete(Integer storagePlatform, String fileUrl, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(storagePlatform);
        uploadFace.delete(fileUrl, upload);
    }


    /**
     * 删除文档
     *
     * @param fileUrls 删除文件地址
     * @param upload   上传配置
     */
    public void delete(Integer storagePlatform, Collection<String> fileUrls, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(storagePlatform);
        uploadFace.delete(fileUrls, upload);
    }


    /**
     * 获取下载地址
     */
    public String getDownloadUrl(Integer storagePlatform, String docUrl, int expireSeconds, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(storagePlatform);
        return uploadFace.getDownloadUrl(docUrl, expireSeconds, upload);
    }

    /**
     * 获取预览地址
     *
     * @return 预览地址
     */
    public String getPreviewConfig(Integer storagePlatform, String docUrl, int expireSeconds, Upload upload) {
        // 获取存储平台
        UploadFace uploadFace = getUploadFace(storagePlatform);
        return uploadFace.getPreviewConfig(docUrl, expireSeconds, upload);
    }

    /**
     * 获取存储通道
     *
     * @param storagePlatform 存储平台
     * @return 存储通道
     */
    private UploadFace getUploadFace(Integer storagePlatform) {
        StoragePlatformEnum storagePlatformEnum = StoragePlatformEnum.byCode(storagePlatform);
        if (Objects.isNull(storagePlatformEnum)) {
            throw new BaseException("存储平台错误");
        }
        UploadFace uploadFace = uploadFaceMap.get(storagePlatformEnum.getMode());
        if (uploadFace == null) {
            throw new BaseException("存储平台错误");
        }
        return uploadFace;
    }

}
